﻿//-----------------------------------------------------------------------
// <copyright file="ExceptionDetailSerializerUnitTest.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics
{
    using System;
    using System.Reflection;
    using System.Collections;
    using System.ServiceModel;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Provides unit testing for the <see cref="T:CodePlex.Diagnostics.ExceptionSerializer"/> class.
    /// </summary>
    [TestClass]
    public sealed class ExceptionDetailSerializerUnitTest
    {
        #region public ExceptionDetailSerializerUnitTest()

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ExceptionDetailSerializerUnitTest"/> class.    
        /// </summary>
        public ExceptionDetailSerializerUnitTest()
        {
        }

        #endregion

        #region public void ExceptionDetailSerializationUnitTest()

        /// <summary>
        /// Unit test to exercise the serialization and deserialization of <see cref="T:System.ServiceModel.ExceptionDetail"/> instances.
        /// </summary>
        [TestMethod]
        public void ExceptionDetailSerializationUnitTest()
        {
            try
            {
                // force a DivideByZeroException to use as an Exception to be serialized and deserialized.
                for (int index = 0; index < 10; index++)
                {
                    index /= index;
                }

                Assert.Inconclusive("ExceptionSerializationUnitTest is inconclusive, failed to generate the sample DivideByZeroException.");
            }
            catch (DivideByZeroException ex)
            {
                // initialize an ExceptionDetail instance based upon the DivideByZeroException.
                ExceptionDetail detail = new ExceptionDetail(ex);

                // serialize the DivideByZeroException using the ExceptionSerializer class.
                string soap = ExceptionDetailSerializer.Serialize(detail);

                // ensure that the Soap encoded Exception is not null or an empty string. 
                Assert.IsFalse(string.IsNullOrEmpty(soap), "Serialized exception is either null or an empty string.");

                ExceptionDetail deserialized = ExceptionDetailSerializer.Deserialize(typeof(ExceptionDetail), soap) as ExceptionDetail;

                // ensure that the deserialized Exception is not null, if the 'as' cast fails then 'deserialized' will unfortunately be null.
                Assert.IsNotNull(deserialized);

                Assert.AreEqual<string>(detail.HelpLink, deserialized.HelpLink, "Serialized exception contains an invalid HelpLink.");
                Assert.AreEqual<string>(detail.Message, deserialized.Message, "Serialized exception contains an invalid Message.");
                Assert.AreEqual<string>(detail.StackTrace, deserialized.StackTrace, "Serialized exception contains an invalid StackTrace.");
                Assert.AreEqual<string>(detail.Type, deserialized.Type, "Serialized exception contains an invalid Type.");
            }
        }

        #endregion
    }
}

